package com.example.taobaolianmeng.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.taobaolianmeng.R;
import com.example.taobaolianmeng.base.BaseFragment;
import com.example.taobaolianmeng.model.domain.SelectedContent;
import com.example.taobaolianmeng.model.domain.SelectedPageCategory;
import com.example.taobaolianmeng.presenter.ISelectedPagePresenter;
import com.example.taobaolianmeng.presenter.ITicketPresenter;
import com.example.taobaolianmeng.ui.activity.TicketActivity;
import com.example.taobaolianmeng.ui.adapter.SelectedPageContentAdapter;
import com.example.taobaolianmeng.ui.adapter.SelectedPageLeftAdapter;
import com.example.taobaolianmeng.utils.PresenterManger;
import com.example.taobaolianmeng.view.ISelectedPageCallback;

import java.util.List;

import butterknife.BindView;

public class SelectedFragment extends BaseFragment implements ISelectedPageCallback, SelectedPageLeftAdapter.OnLeftItemClickListener, SelectedPageContentAdapter.OnSelectedPageContentItemClickListener {

    private static final String TAG = "SelectedFragment" ;
    private ISelectedPagePresenter mSelectedPagePresenter;
    private SelectedPageLeftAdapter mLeftAdapter;

    @BindView(R.id.left_category_list)
    public RecyclerView leftCategoryList;

    @BindView(R.id.right_content_list)
    public RecyclerView rightContentList;

    @BindView(R.id.fragment_bar_title_tv)
    public TextView barTitleTv;

    private SelectedPageContentAdapter mRightAdapter;


    @Override
    protected void initPresenter() {
        super.initPresenter();
        mSelectedPagePresenter = PresenterManger.getInstance().getSelectedPagePresenter();
        mSelectedPagePresenter.registerViewCallback(this);
        mSelectedPagePresenter.getCategories();
    }

    @Override
    protected void onRetryClick() {
        //重试
        if(mSelectedPagePresenter !=null){
            mSelectedPagePresenter.reloadContent();
        }
    }

    @Override
    protected void release() {
        super.release();
        if (mSelectedPagePresenter != null) {
            mSelectedPagePresenter.unregisterViewCallback(this);
        }
    }

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_selected;
    }

    //重写load方法
    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragement_with_bar_layout,container,false);
    }

    @Override
    protected void initView(View rootView) {
        setupState(State.SUCCESS);
        leftCategoryList.setLayoutManager(new LinearLayoutManager(getContext()));
        mLeftAdapter = new SelectedPageLeftAdapter();
        leftCategoryList.setAdapter(mLeftAdapter);

        rightContentList.setLayoutManager(new LinearLayoutManager(getContext()));
        mRightAdapter = new SelectedPageContentAdapter();
        rightContentList.setAdapter(mRightAdapter);
        barTitleTv.setText("精选宝贝");
    }

    @Override
    protected void initListener() {
        super.initListener();
        mLeftAdapter.setOnLeftItemClickListener(this);
        mRightAdapter.setOnSelectedPageContentItemClickListener(this);
    }

    @Override
    public void onCategoriesLoaded(SelectedPageCategory categories) {
        setupState(State.SUCCESS);
        mLeftAdapter.setData(categories);
        //分类内容
        Log.d(TAG,"categories ->"+categories.toString());
        //根据当前选中的分类，获取分类详情内容
        List<SelectedPageCategory.DataBean> data = categories.getData();
        mSelectedPagePresenter.getContentByCategory(data.get(0));


    }

    @Override
    public void onContentLoaded(SelectedContent content) {
        mRightAdapter.setData(content);
        //切换置顶
        rightContentList.scrollToPosition(0);

    }

    @Override
    public void onNetworkError() {
        setupState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setupState(State.LOADING);
    }

    @Override
    public void onEmpty() {

    }

    @Override
    public void onLeftItemClick(SelectedPageCategory.DataBean item) {
        //左边分类点击
        Log.d(TAG,"current selected item ->"+item.getFavorites_title());
        mSelectedPagePresenter.getContentByCategory(item);
    }

    @Override
    public void onContentItemClick(SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean item) {
        //内容被点击
        String title = item.getTitle();
        //详情地址
        String url = item.getCoupon_click_url();
        if(TextUtils.isEmpty(url)){
            //如果没有优惠券直接跳转商品
            url =item.getClick_url();
        }
        String cover = item.getPict_url();
        //拿到ticketPresenter去加载数据
        ITicketPresenter ticketPresenter = PresenterManger.getInstance().getTicketPresenter();
        ticketPresenter.getTicket(title,url,cover);
        startActivity(new Intent(getContext(), TicketActivity.class));
    }
}
